/*
 * IPhysicsEngine.h
 *
 *  Created on: 06.01.2012
 *      Author: Kirill Blinov
 *      Copyright: LGPL
 */

#ifndef IPHYSICSENGINE_H_
#define IPHYSICSENGINE_H_

#include <btBulletDynamicsCommon.h>
#include "IPhysicsObject.h"

class PhysicsEngine
{
friend class PhysicsEngine;
private:
	bool engineInitialized;
	clock_t lastTime;

    btDiscreteDynamicsWorld         *dynamicsWorld;
    btDefaultCollisionConfiguration *collisionConfiguration;
    btAxisSweep3                    *axisSweep;
private:
    static const int MAX_ENGINE_OBJECT_HANDLERS = 256;

	static const glm::vec3 firstWorldCorner;
	static const glm::vec3 secondWorldCorner;
	static const glm::vec3 gravityVector;

	static PhysicsEngine* EngineInstance;

protected:
	PhysicsEngine() :
		engineInitialized(false), lastTime(0), dynamicsWorld(NULL),
		collisionConfiguration(NULL), axisSweep(NULL)
	{};
	PhysicsEngine( glm::vec3 FirstWorldCorner, glm::vec3 SecondWorldCorner, glm::vec3 Gravity );
	virtual ~PhysicsEngine();

public:
	static PhysicsEngine* GetPhysicsEngine()
	{
		if ( NULL == EngineInstance )
		{
			EngineInstance = new PhysicsEngine( firstWorldCorner,
														   secondWorldCorner,
														   gravityVector );
		}
		return EngineInstance;
	}

	static void DestroyInstance()
	{
		delete EngineInstance;
		EngineInstance = NULL;
	}

	virtual void AddPhisicsObject( IPhysicsObject& Object );
	virtual void RemovePhisicsObject( IPhysicsObject& Object );
	virtual void StepSimulate();
};

#endif /* IPHYSICSENGINE_H_ */

